#!/usr/bin/perl -w

=head1 NAME

marccmdd - Command line MARC21 utility

=head1 DESCRIPTION

The marccmd is a command line tool for exploring the content

=head1 SYNOPSIS

marccmd.pl B<[options]> I<arguments> filename

=head1 ARGUMENTS

=over 4

=item L<logfilename>

Specify the filename of the log file.

=back

=head1 OPTIONS

=over 8

=item B<--help>

Show this page.

=item B<--silent>

Don't display data harvested from the repository - only shows a record count.

=item B<--trace>

Turn on trace debugging.


=cut

BEGIN {
	unshift @INC, ".";
}

use vars qw($VERSION $PROTOCOL_VERSION $h);

use lib "../lib";
use lib "lib";

# Pod::Usage, pod2usage() - print a usage message from embedded pod documentation
use Pod::Usage;
use MARC::Record;
use MARC::Batch;
use Encode qw(encode decode);
use utf8;
use open qw( :std :utf8);
use IO::File;



use strict;
use warnings;


binmode(STDOUT,":utf8");

use Getopt::Long;
eval "use Term::ReadLine";
if( $@ )
{
	die "Requires Term::ReadLine perl module\n";
}
eval "use Term::ReadKey";
if( $@ )
{
	die "Requires Term::ReadKey perl module\n";
}


# command line options/arguments
my ($opt_silent, $opt_help);
$opt_silent = 0;
GetOptions (
	'silent' => \$opt_silent,
	'help' => \$opt_help,

);

pod2usage(1) if $opt_help;


print <<EOF;
Welcome to marccmd

Copyright 2011 Chris Keene

Use CTRL+C to quit at any time

---

EOF

my $DEFAULTID = '';

use vars qw($TERM);
$TERM = Term::ReadLine->new($0);


while(1) {

	my $marcfile = shift || $TERM->readline('MARC file to query>','marc.mrc') || next;
	#open marc file

        last;
	
}



&mainloop();

sub mainloop {
	while(1) {
		print "\nMenu\n----\n\n",
			"1. Output all MARC Records\n2. Report on record numbers\n3. fixes and changes\n4. ListMetadataFormats\n5. ListRecords\n6. ListSets\nq. Quit\n\n>";
		my $cmd;
		ReadMode(4);
		$cmd = ReadKey();
		ReadMode(0);
		last unless defined($cmd);
		print $cmd . "\n";
		if( $cmd eq 'q' ) {
			last;
		} elsif($cmd eq '1') {
			eval { GetRecord() };
		} elsif($cmd eq '2') {
			eval { Identify() };
		} elsif($cmd eq '3') {
			eval { ListIdentifiers() };
		} elsif($cmd eq '4') {
			eval { ListMetadataFormats() };
		} elsif($cmd eq '5') {
			eval { ListRecords() };
		} elsif($cmd eq '6') {
			eval { ListSets() };
		}
		if( $@ ) {
			warn "Internal error occurred: $@\n";
		}
	}
}



